package com.gj.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gj.reggie.common.CustomException;
import com.gj.reggie.entity.Category;
import com.gj.reggie.entity.Dish;
import com.gj.reggie.entity.Setmeal;
import com.gj.reggie.mapper.CategoryMapper;
import com.gj.reggie.service.CategoryService;
import com.gj.reggie.service.DishService;
import com.gj.reggie.service.EmployeeService;
import com.gj.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 郭嘉
 * @date 2022/12/28 - 14:46
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    /**
     * 根据id进行删除，删除之前要进行判断是否有关联
     * @param id
     */
    @Override
    public void removeCategory(Long id) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper=new LambdaQueryWrapper<>();
       //查询当前分类是否关联了菜品，如意关联，抛出一个业务异常
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int countForDish=dishService.count(dishLambdaQueryWrapper);
        if(countForDish>0){
            //抛出异常
            throw new CustomException("当前分类项关联菜品，不能删除");
        }
        //查询当前分类是否关联了套餐，如意关联，抛出一个业务异常
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper=new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int countForSetmeal=setmealService.count(setmealLambdaQueryWrapper);
        if(countForDish>0){
            // 抛出异常
            throw new CustomException("当前分类项关联套餐，不能删除");
        }

        super.removeById(id);
    }
}
